// Memory tests.
#include "StdInc.h"

#ifdef _DEBUG

#define NOMINMAX
#include <Windows.h>

#include <sdk/OSUtils.h>

#endif //_DEBUG

void RunMemoryTests( void )
{
#ifdef _DEBUG
    // TEST memory semantics.
    {
        NativePageAllocator allocMan;

        // BURST test.
        {
            std::vector <NativePageAllocator::pageHandle*> handleList;

            size_t memRequest = 0xb0000000;

            for ( size_t n = 0; n < 31; n++ )
            {
                NativePageAllocator::pageHandle *newHandle = allocMan.Allocate( (void*)memRequest, 4096 );

                if ( newHandle )
                {
                    handleList.push_back( newHandle );
                }

                memRequest += 4096;
            }

            allocMan.Free( handleList[ 10 ] );

            handleList[ 10 ] = allocMan.Allocate( NULL, 4096 );

            for ( NativePageAllocator::pageHandle *handle : handleList )
            {
                allocMan.Free( handle );
            }
        }

        // Free alloc test.
        {
            std::vector <NativePageAllocator::pageHandle*> freeHandleList;

            for ( size_t n = 0; n < 31; n++ )
            {
                NativePageAllocator::pageHandle *handle = allocMan.Allocate( NULL, 4096 );

                freeHandleList.push_back( handle );
            }

            __noop();
        }

        // Finicky test.
        {
            NativePageAllocator::pageHandle *allocOne = allocMan.Allocate( NULL, 4096 );
            NativePageAllocator::pageHandle *allocTwo = allocMan.Allocate( NULL, 4096 );
            NativePageAllocator::pageHandle *allocThree = allocMan.Allocate( NULL, 4096 );
            NativePageAllocator::pageHandle *allocFour = allocMan.Allocate( NULL, 4096 );

            NativePageAllocator::pageHandle *primHandle = allocMan.Allocate( (void*)0xc0002000, 0xc000 );
            NativePageAllocator::pageHandle *secHandle = allocMan.Allocate( (void*)0xc0000000, 0x1000 );

            if ( primHandle && secHandle )
            {
                bool couldGrowHandle = allocMan.SetHandleSize( secHandle, 0x2000 );

                assert( couldGrowHandle == true );

                bool couldShrinkHandle = allocMan.SetHandleSize( secHandle, 0x1000 );

                assert( couldShrinkHandle == true );

                bool couldBreakHandleIntegrity = allocMan.SetHandleSize( secHandle, 0x3000 );

                assert( couldBreakHandleIntegrity == false );
            }
        }

        // OK?
    }
#endif //_DEBUG
}